<?php


/* 
 * The include path may change 
 * depending on PHPCodeProvider.php location. 
 */

include_once 'Classes/PHPCodeProvider.php';

/*
 * Create an instance of the CodeGeneratorOptions class
 * using the default options for code generation.
 * It is possible to change the bracing style
 * and also to omit blank lines between the lines.
 */

$options = new CodeGeneratorOptions();
$options->set_BlankLinesBetweenMembers(false);
$options->set_BracingStyle(BracingStyle::KR);

/*
 * Create an instance of the IndentedTextWriter.
 * After the generateCodeFromType call
 * $writer will hold the generated code.
 */

$writer = new IndentedTextWriter();

/*
 * The example class will have two private properties (fields)
 * so we need to set the field attributes to 'private'
 */

$private = new FieldAttributes();
$private->set_private(true);

/*
 * The only class method will be public.
 */

$public = new TypeMemberAttributes();
$public->set_public(true);

/*
 * Create the TypeDeclaration instance that represents the class
 * that will be generated. The class name is 'SuperMemo'.
 */

$class = new TypeDeclaration('SuperMemo');

/*
 * Create the Method class instance names 'resetScore'.
 */

$resetScoreMethod = new Method('resetScore');
$resetScoreMethod->Attributes = $public;

/*
 * Create the first field (property) and set its attributes to private.
 */

$scoreField = new Field('score');
$scoreField->Attributes = $private;

/*
 * Create the second field. This field is initialized to false.
 */

$gameFinishedField = new Field('gameFinished');
$gameFinishedField->Attributes = $private;
$gameFinishedField->InitExpression = new PrimitiveExpression(false);

/*
 * Add all three members to the class.
 */

$class->Members->add($resetScoreMethod);
$class->Members->add($scoreField);
$class->Members->add($gameFinishedField);

/*
 * Create an instance of the PHP code provider class
 * and call its generateCodeFromType() method.
 * After the call $writer will hold the generated PHPcode.
 */

$provider = new PHPCodeProvider();
$provider->generateCodeFromType($class, $writer, $options);

/*
 * Output the generated code using $writer->get_String();
 */

echo 
    '<pre>' . str_replace(
        '<br />', '', highlight_string($writer->get_String(), true)) .
    '<pre/>';

/*
 * The output should be:
 * Note the bracing style and no blank lines between members.
 * 
 * class SuperMemo {
 *   public function resetScore () {
 *   }
 *   private $score;
 *   private $gameFinished = false;
 * }
 * 
 */

?>
